# @todo  模拟优酷 vip 登入，获取登入cookie 并且保存

import requests
import re
import time
import json

# todo  获取token关键信息
#     "loginFormData":{
#         "appName":"youku",
#         "appEntrance":"youku_dark",
#         "_csrf_token":"w5EAit7HHXQi3F6TrG08v4",
#         "umidToken":"24e6f1e4bb8d735f925b67029509d0ef94d092b6",
#         "isMobile":false,
#         "lang":"zh_CN",
#         "returnUrl":"",
#         "hsiz":"sPaCQVr0x9f8hiGtrHXeVQ",
#         "fromSite":23,
#         "bizParams":""
#     },
def getToken():
    url = "https://cnpassport.youku.com/mini_login.htm?lang=zh_cn&appName=youku&appEntrance=youku_dark&styleType=vertical&bizParams=&notLoadSsoView=true&notKeepLogin=false&isMobile=false"
    r = requests.get(url)
    text = r.text.replace('\n', '').replace(' ', '').replace('false', '"false"')
    s = re.search(r'"currentTime":("\d{13}").*"loginFormData":(.*?),"mobile"', text).groups()
    print(s)
    return s


    # loginFormData":{"appName":"youku","appEntrance":"youku_dark","_csrf_token":"Z5b2UY6sxOFrrOfS3s4C45","umidToken":"b731172e74f6fa6b57592543282f11006215f461","isMobile":false,"lang":"zh_CN","returnUrl":"","hsiz":"j4YMePiO1jeylvEgcFWoxg","fromSite":23,"bizParams":""},"mobile":false,"nocaptchaAppKey


def getQrcode():
    token = getToken()
    #二维码开始时间
    start = token[0]
    param = eval(token[1])
    print(param)
    url = "https://cnpassport.youku.com/newlogin/qrcode/generate.do?appName=youku&fromSite=23&appName=youku&appEntrance=youku_dark&_csrf_token={name[_csrf_token]}&umidToken={name[umidToken]}&isMobile=false&lang=zh_CN&returnUrl=&hsiz={name[hsiz]}&fromSite=23&bizParams=&_bx-v=2.0.31".format(name=param)
    print(url)

    # 获取二维码以及一些登入参数  ck   t
    r = requests.get(url)
    qrdata = eval(r.text.replace('false', '"false"').replace('true', '"true"'))
    print(qrdata)
    ck = qrdata['content']['data']['ck']
    t = qrdata['content']['data']['t']
    print(ck)

    url = 'https://cnpassport.youku.com/newlogin/qrcode/query.do?appName=youku&fromSite=23&_bx-v=2.0.31'
    data = {
        't': t,
        'ck': ck,
        'ua': '',  #经过测试ua 不影响结果
        'appName': 'youku',
        'appEntrance': 'youku_dark',
        '_csrf_token': param['_csrf_token'],
        'umidToken': param['umidToken'],
        'isMobile': 'false',
        'lang': 'zh_CN',
        'returnUrl': '',
        'hsiz': param['hsiz'],
        'fromSite': '23',
        'bizParams': '',
        'navlanguage': 'zh-CN',
        'navUserAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'navPlatform': 'Win32',
    }
    print(data)
    # 循环等待扫码情况 扫码成功后保存vip P_pck_rm cookie
    while (True):
        time.sleep(1)
        r = requests.post(url, data=data)
        print(r.text)
        if re.search('loginResult":"success"',r.text):
            print(r.cookies.get_dict())
            f=open('./vipcookie','w',encoding='utf-8')
            f.write(r.cookies.get_dict()['P_pck_rm'])
            f.close()
            break
getQrcode()


